//
//  XHRealNameViewController.m
//  umibus
//
//  Created by 冷山 on 16/6/17.
//  Copyright © 2016年 lexingsoft. All rights reserved.
//

#import "XHRealNameViewController.h"

#import "Services.h"

#import <UIAlertView+Blocks.h>

@interface XHRealNameViewController ()
@property (weak, nonatomic) IBOutlet UIView *ui_realNameView;
@property (weak, nonatomic) IBOutlet UIView *ui_idNumberView;
@property (weak, nonatomic) IBOutlet UIView *ui_certificationView;
@property (weak, nonatomic) IBOutlet UITextField *realNameField;
@property (weak, nonatomic) IBOutlet UITextField *idCardField;

@property (weak, nonatomic) IBOutlet UIButton *ui_certificationButton;
@end

@implementation XHRealNameViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.ui_realNameView.layer.borderWidth = 1;
    self.ui_realNameView.layer.borderColor = [ColorManager colorWithHexString:@"#C4C4C4"].CGColor;
    
    self.ui_idNumberView.layer.borderWidth = 1;
    self.ui_idNumberView.layer.borderColor = [ColorManager colorWithHexString:@"#C4C4C4"].CGColor;
    
    self.ui_certificationButton.layer.masksToBounds = YES;
    self.ui_certificationButton.layer.cornerRadius = 8;

    if ([NSString checkNullString:self.idCard]&&
        [NSString checkNullString:self.realName])
    {
        self.ui_certificationView.hidden = NO;
    }
    else
    {
        self.idCardField.userInteractionEnabled = NO;
        self.realNameField.userInteractionEnabled = NO;
        self.idCardField.text = [NSString stringWithFormat:@"%@",self.idCard];
        self.realNameField.text = [NSString stringWithFormat:@"%@",self.realName];
        self.ui_certificationView.hidden = YES;
        UIBarButtonItem *settingItem=[[UIBarButtonItem alloc] initWithTitle:@"联系客服" style:UIBarButtonItemStylePlain target:self action:@selector(settingAction)];
        self.navigationItem.rightBarButtonItem=settingItem;
    }
}

- (void)settingAction
{
    //电话咨询
    NSString *phoneNumber=@"400-808-3555";
    NSString *title=[NSString stringWithFormat:@"拨打电话:%@",phoneNumber];
    RIButtonItem *cancelItem=[RIButtonItem itemWithLabel:@"取消"];
    RIButtonItem *callItem=[RIButtonItem itemWithLabel:@"呼叫" action:^{
        NSURL *url=[NSURL URLWithString:[NSString stringWithFormat:@"tel://%@",phoneNumber]];
        [[UIApplication sharedApplication] openURL:url];
    }];
    UIAlertView *alertView=[[UIAlertView alloc] initWithTitle:title message:@"" cancelButtonItem:cancelItem otherButtonItems:callItem, nil];
    [alertView show];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (IBAction)pushInfoAction:(id)sender
{
    if ([NSString checkNullString:self.realNameField.text]||
        [NSString checkNullString:self.idCardField.text])
    {
        [PromptAction showInfoPrompt:@"请填写相关认证信息"];
    }
    else if (![NSString validateIdentityCard:self.idCardField.text])
    {
        [PromptAction showInfoPrompt:@"身份号格式不正确"];
    }
    else
    {
        NSDictionary * dic = [[NSDictionary alloc]init];
        dic=@{@"idCard":self.idCardField.text,
              @"userName":self.realNameField.text};
        [ServiceManager refreshUserInfo:dic success:^(NSDictionary *data)
         {
             [PromptAction showSuccessPrompt:@"提交信息成功"];
             [self.navigationController popViewControllerAnimated:YES];
         } failure:^{
             [PromptAction showSuccessPrompt:@"提交信息失败"];
         }];
    }
}
- (IBAction)clickBgView:(id)sender
{
    [self.view endEditing:YES];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
